﻿Ext.define("GS.controller.Event", {
    extend: "Ext.app.Controller",

    config: {
        refs: {
            mainView: "main",
            eventView: "viewevent",
            addEventView: "addevent",
            eventList: '#eventlist',
            EventForm: '#eventform',
            btnCancelEvent: "#btnCancelEvent",
            btnAddEvent: "#btnAddEvent",
            btnSubmitEvent: "#btnSubmitEvent",
            btnBackViewEvent: "#btnBackViewEvent"
        },
        control: {
           btnCancelEvent: {
                tap: "onCancelEvent"
           },
           btnAddEvent: {
                tap: "onAddEvent"
           },
           btnSubmitEvent: {
                tap: "onSubmitEvent"
           },
           btnBackViewEvent: {
                tap: "onBackViewEvent"
           },
           eventList: {
                itemtap: "onEventView"
           }
        }
    },
    onEventView: function (list, index, element, record) {
        this.getEventView().updateEvent(record.data);
        Ext.Viewport.setActiveItem(this.getEventView());
    },
    onBackViewEvent: function () {
        Ext.Viewport.setActiveItem(this.getMainView());
    },
    onAddEvent: function () {
        Ext.Viewport.setActiveItem(this.getAddEventView());
    },
    onCancelEvent: function (id) {
        Ext.Viewport.setActiveItem(this.getMainView());
    },
    onSubmitEvent: function () {
        var values = this.getEventForm().getValues();

        var user = Ext.create('GS.store.UserData');

        user.load();

        var user_id = user.getAt(0).get('user_id');
        var school_id = user.getAt(0).get('school_id');

        var model = Ext.create('GS.model.event.Event', values);
                                
        var errors = model.validate();
        if(errors.isValid()){
            Ext.data.JsonP.request({
                url: GS.util.Config.getServerUrl()+'AddEvent.php',
                callbackKey: "callback",
                params: { school_id: school_id, user_id: user_id, title: values.title, description: values.description, location: values.location, date: values.date },

                success: function (response) {
                    var text = response.responseText;
                    Ext.Msg.alert('Success', 'Kommentar tilføjet');
                },

                failure: function (response) {
                    Ext.Msg.alert('Error', 'Error while submitting the form');
                    //console.log(response.responseText);
                } 
            });

            Ext.getCmp('eventlist').getStore().load();
            Ext.Viewport.setActiveItem(this.getMainView());
        } else {
            var errorMessage = "";
            Ext.each(errors.items,function(rec,i){
                errorMessage += rec.config.message+"<br/>";
            });
            Ext.Msg.alert("Fejl", errorMessage, function(){});
            return false;
        }  
    },
});